/**
 * 组合模式又名整体-部分模式，是一种树形状的实现
 * 通过上下级的关系来实现组合
 */

 class Employee{
     constructor(name,dept,sal){
        this.name = name
        this.dept = dept
        this.salary = sal
        this.subordinates = []
     }
     add(employee){
        this.subordinates.push(employee)
     }
     remove(employee){
         this.subordinates.push(this.subordinates.indexOf(employee),1)
     }
     toString(){
        return ("Employee :[ Name : "+ this.name
        +", dept : "+ this.dept + ", salary :"
        + this.salary+" ]");
     }
     getSubordinates(){
        return this.subordinates;
      }

     static printAllEmployee(employee){
        console.log(employee.toString())
        const subordinates = employee.getSubordinates()
        for(let it of subordinates){
            this.printAllEmployee(it)
        }
     }
 }
 //将相同的对象进行组合
 //通过组合模式，一个递归就可以访问完全部节点
 const CEO = new Employee("John","CEO", 30000);

 const headSales = new Employee("Robert","Head Sales", 20000); 
 const headMarketing = new Employee("Michel","Head Marketing", 20000);
 
 const clerk1 = new Employee("Laura","Marketing", 10000);
 const clerk2 = new Employee("Bob","Marketing", 10000);
 
 const salesExecutive1 = new Employee("Richard","Sales", 10000);
 const salesExecutive2 = new Employee("Rob","Sales", 10000);
 
 CEO.add(headSales);
 CEO.add(headMarketing);
 
 headSales.add(salesExecutive1);
 headSales.add(salesExecutive2);
 
 headMarketing.add(clerk1);
 headMarketing.add(clerk2);

 Employee.printAllEmployee(CEO)